package club.adger.service.impl;

import club.adger.dao.EmployeesMapper;
import club.adger.entity.Employees;
import club.adger.service.LoginService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Adger
 * @Date: 2020/07/03/16:25
 * @Service 交给 Spring来管理
 * 这是登录的服务层
 */
@Service
public class LoginServiceImpl implements LoginService {
    /**
     * 注入持久层
     */
    @Autowired
    private EmployeesMapper employeesMapper;

    /**
     * new 出JSONObject为了返回结果值
     * @param request
     * @return
     */
    @Override
    public JSONObject login(HttpServletRequest request) {
        System.out.println(employeesMapper);
        JSONObject result = new JSONObject();
        String username  =  request.getParameter("username");
        String password  =  request.getParameter("pwd");
        System.out.println("账号：" + username + "密码：" + password);
        List<Employees> loginList = employeesMapper.login(username, password);
        //如果查出来的数据 == 0 就是找不到这个人
        if(loginList.size() == 0){
            result.put("message","账号密码错误");
            result.put("success",false);
        }else if(loginList.size()>1){
            result.put("message","数据异常");
            result.put("success",false);
        }else {
            result.put("message","登录成功");
            request.getSession().setAttribute("currentUserName",username);
            result.put("success",true);
        }
        return result;
    }
}
